package com.awesome.common.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.awesome.common.util.log.ShowLogUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date:2020/12/16 13:26
 * Description:存储 SharedPreferences 的工具类
 */
public class SharedPreferencesHelper {

    private static final String TAG = "SharedPreferencesHelper";
    private SharedPreferences mSharedPreferences;

    public SharedPreferencesHelper(Context context, String name) {
        mSharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:26
     * Description:存 int 类型数据
     */
    public void putInt(String key, int value) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 int 类型数据,重载 {@link #getInt(String, int)}
     */
    public int getInt(String key) {
        return getInt(key, -1);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 int 类型数据
     *
     * @param key
     * @param defaultValue key 对应的值不存在时的默认值
     * @return
     */
    public int getInt(String key, int defaultValue) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return defaultValue;
        }
        return mSharedPreferences.getInt(key, defaultValue);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:26
     * Description:存 String 类型数据
     */
    public void putString(String key, String value) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 String 类型数据,重载 {@link #getString(String, String)}
     */
    public String getString(String key) {
        return getString(key, null);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 String 类型数据
     *
     * @param key
     * @param defaultValue key 对应的值不存在时的默认值
     * @return
     */
    public String getString(String key, String defaultValue) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return defaultValue;
        }
        return mSharedPreferences.getString(key, defaultValue);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:26
     * Description:存 long 类型数据
     */
    public void putLong(String key, long value) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 long 类型数据,重载 {@link #getLong(String, long)}
     */
    public long getLong(String key) {
        return getLong(key, -1);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 long 类型数据
     *
     * @param key
     * @param defaultValue key 对应的值不存在时的默认值
     * @return
     */
    public long getLong(String key, long defaultValue) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return -1;
        }
        return mSharedPreferences.getLong(key, defaultValue);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:26
     * Description:存 boolean 类型数据
     */
    public void putBoolean(String key, boolean value) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 boolean 类型数据,重载 {@link #getBoolean(String, boolean)}
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 boolean 类型数据
     *
     * @param key
     * @param defaultValue key 对应的值不存在时的默认值
     * @return
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return false;
        }
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:26
     * Description:存 String 类型的集合数据
     */
    public void putStringSet(String key, Set<String> value) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putStringSet(key, value);
        editor.apply();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 String 类型集合数据,重载 {@link #getStringSet(String, Set<String>)}
     */
    public Set<String> getStringSet(String key) {
        return getStringSet(key, new HashSet<>());
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:27
     * Description:取 String 类型集合数据
     *
     * @param key
     * @param defaultValue key 对应的值不存在时的默认值
     * @return
     */
    public Set<String> getStringSet(String key, Set<String> defaultValue) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return null;
        }
        return mSharedPreferences.getStringSet(key, defaultValue);
    }

    /**
     * Author：JFangZ
     * Email： janezhang1901@163.com
     * Date:2019/11/5 16:45
     * Description:是否有存储某个 key
     */
    public boolean contains(String key) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return false;
        }
        return mSharedPreferences.contains(key);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:30
     * Description:删除某个 key 对应的数据
     */
    public void remove(String key) {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        editor.apply();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 13:30
     * Description:清空所有数据
     */
    public void clearAll() {
        if (mSharedPreferences == null) {
            ShowLogUtil.error(TAG, "please call init first");
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
}